# -*- coding: utf-8 -*-

import sqlite3
import csv
import time

class DBGenerator:
    def __init__(self):
        pass

    def generate(self):
        t1 = time.clock()
        conn = sqlite3.connect("TP1.db")
        conn.text_factory = str
        c = conn.cursor()

        for i in range (0, 11):
            f = 0
            reader = csv.reader(open("./rebcsv/reb{0}.csv".format(i), "rb"))
            c.execute('''drop table if exists reb{0}'''.format(i))
            c.execute('''create table if not exists reb{0} (cod_docent integer,\
                        nome text, ult_grau text, curso text, cat text,\
                        regime text, ano_lectivo integer,\
                        tipo_estabelecimento text,\
                        nome_estabelecimento text)'''.format(i))

            for x in reader:
                if (f == 0):
                    f = f + 1
                    pass
                else:
                    l = []
                    for s in x:
                        s.decode("iso-8859-1")
                        l.append(s)
                        pass
                    c.execute('''insert into reb{0} values \
                                    (?,?,?,?,?,?,?,?,?)'''.format(i), l)
                    pass
            pass
                
        conn.commit()
        conn.close()

        t2 = time.clock()
        print "DBGeneration = ", t2-t1
        pass

##gen = DBGenerator().generate()
